import { useUserStore } from "@/store/user";

// 应用初始化
export const initApp = () => {
  const userStore = useUserStore();
  // 初始化用户信息
  userStore.initUserInfo();
  // 检查登录状态
  const isLoggedIn = userStore.checkLoginStatus();
  return {
    isLoggedIn,
    userStore,
  };
};

// 检查页面访问权限
export const checkPageAuth = (requireAuth = true) => {
  const userStore = useUserStore();
  const isLoggedIn = userStore.isLoggedIn;
  if (requireAuth && !isLoggedIn) {
    // 需要登录但未登录，跳转到登录页
    uni.reLaunch({
      url: "/pages/login/login",
    });
    return false;
  }
  return true;
};

// 退出登录并跳转
export const logoutAndRedirect = () => {
  const userStore = useUserStore();
  userStore.logout();
  uni.reLaunch({
    url: "/pages/login/login",
  });
};
